<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Jobs');
	}

	function index()
	{
		$this->load->helper('url');
		$this->load->library('Menu');
		$menu = new Menu;
		
		$data = $menu->gen();
		
		$this->load->view('index', $data);
	}
	
	function register()
	{
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		
		$data = $menu->gen();
		$data['isEdit'] = false;
		$data['user'] = $user;                                
//                print_r($user);die;
		if ($user->id > 0) {
			// edit
			$data['isEdit'] = true;
			$data['update'] = $this->input->get('update');
			$data['userinfo'] = $user;
		}
                
		//} else {
			if ($this->input->post('doregister')) {
				if ($data['isEdit']) {
					$errors = $user->updateAccount($data['userinfo']->id, $this->input->post('password'), $this->input->post('chpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), $this->input->post('company_name'));
				} else {
					if ($this->input->post('quickregister')) {
						$errors = $user->createAccount($this->input->post('email'), '', '', '', '', '', true);
					} else {
						$errors = $user->createAccount($this->input->post('email'), $this->input->post('password'), $this->input->post('chpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), false, 0, $this->input->post('company_name'));
					}
				}
				if ($errors['noErrors']) {
					// Register/edit complete
					if ($data['isEdit']) {
						//redirect('/profile');
						$this->load->view('user-edit-msg', $data);
					} else {
						$this->load->view('user-signup-complete', $data);
					}
				} else {
					$data['regErrors'] = $errors;
					
					$this->load->view('user-signup', $data);
				}
			} else {
				$regErrors['name'] = false;
				$regErrors['email'] = false;
				$regErrors['emailtaken'] = false;
				$regErrors['password'] = false;
				$regErrors['passwords'] = false;
				$regErrors['phone'] = false;
				$regErrors['address'] = false;
				$data['regErrors'] = $regErrors;
				
				$this->load->view('user-signup', $data);
			}
		//}
	}
	
	function registerComplete() {
		$this->load->helper('url');
		$this->load->library('Menu');
		$menu = new Menu;
		
		$data = $menu->gen();
		
		$this->load->view('user-signup-complete', $data);
	}
	
	function login()
	{   
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('Menu');
		$menu = new Menu;
		$user = &Users::getInstance();
		$data = $menu->gen();
		if ($user->id > 0) {
			redirect('/');
		} else {
			
			if ($this->input->post('dologin')) {
				$email = $this->input->post('email');
                $sql = "SELECT * FROM `users` WHERE `email` = '$email'";
				$elapse = $this->db->query($sql);	
				$trips= $elapse->result();
				$count_elapse = count($trips);
				if($count_elapse >0){
						$id = $trips[0];
						$lock  = $id->Block;
						if($lock == 0){
								if ($user->login($this->input->post('email'), $this->input->post('password'), $this->input->post('joblogin'))) {
								$ua=$this->getBrowser();
								$sql = "INSERT INTO `session` (`uid`,`time`,`browser`,`action`,`notice`) VALUE (".$user->id.",NOW(),'".$ua['name']."','login','') ";
								$this->db->query($sql);
								redirect('/');

							} else {
								$data['loginError'] = true;
							}
						}
						else{
							die("This email has been blocked by admin");
							//redirect('http://bookabus.sg/dev');
						}
				}		   
			}
			
			$this->load->view('user-login', $data);
		}
		
	}
	
	function getBrowser() 
	{ 
		$u_agent = $_SERVER['HTTP_USER_AGENT']; 
		$bname = 'Unknown';
		$platform = 'Unknown';
		$version= "";

		//First get the platform?
		if (preg_match('/linux/i', $u_agent)) {
			$platform = 'linux';
		}
		elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
			$platform = 'mac';
		}
		elseif (preg_match('/windows|win32/i', $u_agent)) {
			$platform = 'windows';
		}
		
		// Next get the name of the useragent yes seperately and for good reason
		if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
		{ 
			$bname = 'Internet Explorer'; 
			$ub = "MSIE"; 
		} 
		elseif(preg_match('/Firefox/i',$u_agent)) 
		{ 
			$bname = 'Mozilla Firefox'; 
			$ub = "Firefox"; 
		} 
		elseif(preg_match('/Chrome/i',$u_agent)) 
		{ 
			$bname = 'Google Chrome'; 
			$ub = "Chrome"; 
		} 
		elseif(preg_match('/Safari/i',$u_agent)) 
		{ 
			$bname = 'Apple Safari'; 
			$ub = "Safari"; 
		} 
		elseif(preg_match('/Opera/i',$u_agent)) 
		{ 
			$bname = 'Opera'; 
			$ub = "Opera"; 
		} 
		elseif(preg_match('/Netscape/i',$u_agent)) 
		{ 
			$bname = 'Netscape'; 
			$ub = "Netscape"; 
		} 
		
		// finally get the correct version number
		$known = array('Version', $ub, 'other');
		$pattern = '#(?<browser>' . join('|', $known) .
		')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
		if (!preg_match_all($pattern, $u_agent, $matches)) {
			// we have no matching number just continue
		}
		
		// see how many we have
		$i = count($matches['browser']);
		if ($i != 1) {
			//we will have two since we are not using 'other' argument yet
			//see if version is before or after the name
			if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
				$version= $matches['version'][0];
			}
			else {
				$version= $matches['version'][1];
			}
		}
		else {
			$version= $matches['version'][0];
		}
		
		// check if we have a number
		if ($version==null || $version=="") {$version="?";}
		
		return array(
			'userAgent' => $u_agent,
			'name'      => $bname,
			'version'   => $version,
			'platform'  => $platform,
			'pattern'    => $pattern
		);
	} 

	

	
	function logout()
	{
		$this->load->helper('url');
		$user =& Users::getInstance();
		
		$user->logout();
		
		redirect('/');
	}
	
	public function back(){		
		$this->load->library('session');
		$this->load->helper('url');
		$user =& Users::getInstance();		
		$user->logout();
		$user->login($this->session->userdata['omail'],$this->session->userdata['opass'],0);		
		redirect('/');			
	}
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */